import { createRouter, createMemoryHistory } from 'vue-router'
const router = createRouter({
  history: createMemoryHistory(),
  routes: [
    { path: '/', redirect: '/home' , component: () => import('../view/home.vue') },
    { path: '/home', component: () => import('../view/home.vue'), redirect: '/home/user',
      children: [
        { path: 'user', component: () => import('../view/home/user.vue') },
        { path: 'authority', component: () => import('../view/home/authority.vue') },
        { path: 'goods', component: () => import('../view/home/goods.vue') },
        { path: 'order', component: () => import('../view/home/order.vue') },
        { path: 'system', component: () => import('../view/home/system.vue') },
        { path: 'user/:id', component: () => import('../view/userAbout.vue'), props: true }
      ]
    },
    { path: '/login', component: () => import('../view/login.vue') }
  ]

})

router.beforeEach((to, from) => {
  const token = localStorage.getItem('token')
  if(to.fullPath !== '/login' && !token) return '/login'
})

export default router
